package com.socket.TCP.server;

import com.socket.TCP.thread.ServerThread;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by wangyg on 2016/10/31.
 */
public class Server {
	public static void main(String[] args) throws IOException {
/*		ServerSocket server = new ServerSocket(9999);
		System.out.println("服务器即将启动，等待客户端的连接。。。");
		Socket socket= server.accept();
		InputStream is = socket.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		String info=null;
		while((info=br.readLine())!=null){
			System.out.println("我是服务器，客户端信息为："+info);
		}
		socket.shutdownInput();
		OutputStream os = socket.getOutputStream();
		PrintWriter pw = new PrintWriter(os);
		pw.write("欢迎登陆");
		pw.flush();
		socket.shutdownOutput();

		pw.close();
		os.close();


		br.close();
		isr.close();
		is.close();
		socket.close();
		server.close();*/

		//修改为多线程版本的server
		ServerSocket server = new ServerSocket(9999);
		System.out.println("服务器即将启动，等待客户端的连接。。。");
		Socket socket = null;
		int count=0;

		while(true){
			socket = server.accept();
			ServerThread st= new ServerThread(socket);
			st.start();
			count++;
			System.out.println("当前已连接客户端数量为："+count+"个");
			System.out.println("当前线程名为："+st.getName());
		}

	}
}
